import {mapGetters} from 'vuex';
import urlHelper from '@/lib/url_helper';

let UserInfo = {
  install (Vue, options) {
    Vue.mixin({
      computed: {
        ...mapGetters({
          $_isLogin: 'isLogin',
          $_username: 'username',
          $_photo: 'photo',
          $_token: 'token',
          $_userId: 'userId',
          $_headerCss: 'headerCss'
        })
      }
    });

    Vue.prototype.$_jumpToLogin = function (redirectInfo) {
      let redirectUrl;
      let type = 'view';
      if (redirectInfo.url) {
        redirectUrl = redirectInfo.url;
        type = 'url';
      } else if (redirectInfo.viewName) {
        let routeInfo = this.$router.resolve({ name: redirectInfo.viewName });
        if (routeInfo && routeInfo.resolved && routeInfo.resolved.path) {
          redirectUrl = routeInfo.resolved.path;
        }
      }
      this.$router.push({
        name: 'login',
        query: {
          path: redirectUrl,
          query: urlHelper.mergeUrlParam(redirectInfo.query || {}),
          appendUserId: redirectInfo.appendUserId || false,
          type: type
        }
      });
    };

    Vue.prototype.$_jompToLogout = function () {
      this.$store.dispatch('logout');
      this.$router.push({name: 'index'});
    };
  }
};

export default UserInfo;
